/**
 * 
 */
package com.thor.palmmall.rest.servicebill;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.servicebill.converter.AServiceBillConverter;
import com.thor.palmmall.rest.servicebill.converter.SServiceBill4CreateConverter;
import com.thor.palmmall.rest.servicebill.converter.SServiceBillConverter;
import com.thor.palmmall.rest.servicebill.converter.SServiceBillFinishParamConverter;
import com.thor.palmmall.rest.servicebill.converter.SServiceBillQueryParamConverter;
import com.thor.palmmall.spi.basic.SBasicService;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.servicebill.SServiceBill;
import com.thor.palmmall.spi.servicebill.SServiceBillService;

/**
 * @author NuoSa
 *
 * @since 1.0
 */
public class AServiceBillServiceImpl implements AServiceBillService {

  @Override
  public String create(AServiceBill ServiceBill, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(ServiceBill, "ServiceBill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sServiceBillService.create(
        SServiceBill4CreateConverter.getInstance().convert(ServiceBill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法录入服务申请单：" + resp.getMessage());
  }

  @Override
  public AServiceBill get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    SResponse<SServiceBill> resp = sServiceBillService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return AServiceBillConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法找到指定ID的服务申请单：" + resp.getMessage());
  }

  @Override
  public String save(AServiceBill ServiceBill, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(ServiceBill, "ServiceBill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");
    SResponse<String> resp = sServiceBillService.save(
        SServiceBillConverter.getInstance().convert(ServiceBill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法保存服务申请单：" + resp.getMessage());

  }

  @Override
  public String finish(AServiceBillFinishParam param) throws ThorServiceException {
    SResponse<String> resp = sServiceBillService.finish(
        SServiceBillFinishParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法完成服务申请单：" + resp.getMessage());
  }
  
  @Override
  public QueryResult<AServiceBill> query(AServiceBillQueryParam param) throws ThorServiceException {

    SResponse<SQueryResult<SServiceBill>> resp = sServiceBillService
        .query(SServiceBillQueryParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(), AServiceBillConverter.getInstance());
    else
      throw new ThorServiceException("无法查询到服务申请单：" + resp.getMessage());
  }

  @Override
  public String[] queryOutgoingState() throws ThorServiceException {
    SResponse<String[]> resp = sServiceBillService.queryOutgoingState();
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("查询服务申请单流程状态失败：" + resp.getMessage());
    }
  }
  
  @Override
  public List<String> typeTag(String category, String subType) throws ThorServiceException {
    SResponse<List<String>> resp = sBasicService.typeTag(category, subType);
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("获取模块配置的基础数据失败：" + resp.getMessage());
    }
  }
  

  @Autowired
  private SServiceBillService sServiceBillService;
  @Autowired
  private SBasicService sBasicService;



}
